<template>
  <div class="dashboard-editor-container">
    <div>
      <el-card>
        <el-form :model="ruleForm" style="margin-top:10px;" size="mini" label-position="top" ref="ruleForm">
          <el-row :gutter="10">
            <el-col :lg="4" :md="6">
              <el-form-item label="表列字段" prop='COLUMNID'>
                <el-select v-model="ruleForm.COLUMNID" size="mini" filterable clearable
                  placeholder="请选择表列字段">
                  <el-option v-for="item in dataOption" :key="item.FIELDKEY" :label="item.FIELDKEY+'-'+item.FIELDVALUE"
                    :value="item.FIELDKEY"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="表列名" prop='COLUMNNAME'>
                <el-input size="mini" placeholder="请输入表列字段" v-model="ruleForm.COLUMNNAME"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="ID" prop='ID'>
                <el-input size="mini" placeholder="请输入ID" v-model="ruleForm.ID"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="规则条件" prop='ROLELEVEL'>
                <el-input size="mini" placeholder="请输入规则条件" v-model="ruleForm.ROLELEVEL"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="添加名" prop='ROLELEVELNAME'>
                <el-input size="mini" placeholder="请输入添加名" v-model="ruleForm.ROLELEVELNAME"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :md="12" class="searchBtnMarginTop">
              <el-button type="primary" icon="el-icon-search" :loading="loading" size="mini"
                @click="queryRuleDictionary">查询
              </el-button>
              <el-button type='warning' size='mini' icon='el-icon-refresh' @click="clearForm('ruleForm')">重置</el-button>
              <el-button type="success" size="mini" icon="el-icon-plus" @click="openEditDialog('','add')">新增字段
              </el-button>
            </el-col>
          </el-row>
        </el-form>
        <el-table :data="tableData" border highlight-current-row class="tableMarginTop">
          <el-table-column prop="ID" align="center" label="ID"></el-table-column>
          <el-table-column prop="COLUMNID" align="center" label="表列字段"></el-table-column>
          <el-table-column prop="COLUMNNAME" align="center" label="表列名"></el-table-column>
          <el-table-column prop="ROLELEVEL" align="center" label="规则条件"></el-table-column>
          <el-table-column prop="ROLELEVELNAME" align="center" label="添加名"></el-table-column>
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="scope">
              <div>
                <el-button type="warning" size="mini" icon="el-icon-edit" @click="openEditDialog(scope.row,'update')">
                  修改</el-button>
                <el-button type="danger" size="mini" icon="el-icon-delete" @click="delImportData(scope.row)">删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-col>
            <el-pagination style="text-align: right; margin-top:20px;" :page-sizes="[10, 20, 50, 100]"
              :page-size="pageSize" @size-change="handleInSizeChange" @current-change="handleInCurrentChange"
              :current-page="currpage" background layout="sizes, total, prev, pager, next, jumper" :total="tableTotal">
            </el-pagination>
          </el-col>
        </el-row>
      </el-card>
    </div>
    <component :is="dialogList.editDialog" :option="editOptions" @refreshTable="queryRuleDictionary"></component>
  </div>
</template>

<script>
  // 发送接口
  import getGlobalParams from "@/common/utils/getGlobalParams";
  import request from "@/common/utils/requestQC";
  import queyData from "@/message/QCManage/sampleManage/sampleDictionary/sampleDictionaryQuery";
  import delData from "@/message/QCManage/sampleManage/sampleDictionary/sampleDictionaryDel";
  import toolBox from "@/common/utils/toolBox";
  import editDialog from "./dialog/editDialog";
  import dictionary from '@/message/wfm/baseData/dictionaryManage/dictionaryQuery'
  import requestWFM from "@/common/utils/requestWFM";
  //批量上传
  export default {
    name: '',
    data() {
      return {
        dataOption: [],
        channelOption: [], //表列名类型
        dialogList: {
          editDialog
        },
        editOptions: {
          isShow: false,
          title: '',
          row: {},
          flag: ''
        },
        tableTotal: 1,
        currpage: 1,
        pageSize: getGlobalParams.get("PageSize"),
        tableData: [],
        ruleForm: {
          COLUMNNAME: '', //表列名
          COLUMNID: '', //字段在文件中的表列字段
          ID: '',
          ROLELEVEL: '', //规则条件
          ROLELEVELNAME: '', //添加名
        },
        loading: false,
      }
    },
    computed: {},
    created() {

    },
    mounted() {
      this.dictionaryQuery('DBCOL').then(value => this.dataOption = value)
      this.queryRuleDictionary()
    },
    watch: {},
    methods: {
      async dictionaryQuery(type) {
        let resBody = new dictionary();
        resBody.data.FIELDCODE = type
        resBody.data.CurrPage = 1
        resBody.data.PageRowNum = 400
        let response = await requestWFM(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          return response.BODY.FIEDATA;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      dataFormat(row, column) {
        //枚举设备形式
      },
      //打开修改新增Dialog
      async openEditDialog(row, flag) {
        this.editOptions.channelOption = this.channelOption
        this.editOptions.dataOption = this.dataOption
        this.editOptions.flag = flag
        if (flag == 'add') {
          this.editOptions.isShow = true
          this.editOptions.title = '新增抽样规则字典'
        } else {
          this.editOptions.isShow = true
          this.editOptions.title = '修改抽样规则字典'
          this.editOptions.row = row
        }
      },
      //前端分页
      handleInCurrentChange(val) {
        this.currpage = val;
        // console.log(`当前页: ${val}`);
        this.queryRuleDictionary()
      },
      handleInSizeChange(val) {
        this.pageSize = val;
        this.queryRuleDictionary()
      },
      //删除导入字段
      delImportData(row) {
        this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let resBody = new delData()
          resBody.data.ID = row.ID
          let response = await request(resBody);
          if (response.SYS_HEAD.ReturnCode == "000000") {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.queryRuleDictionary()
          } else {
            this.$message({
              type: "error",
              message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
            })
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      // 查询规则字典
      async queryRuleDictionary() {
        let resBody = new queyData()
        resBody.data.COLUMNNAME = this.ruleForm.COLUMNNAME
        resBody.data.COLUMNID = this.ruleForm.COLUMNID
        resBody.data.ID = this.ruleForm.ID
        resBody.data.ROLELEVEL = this.ruleForm.ROLELEVEL
        resBody.data.ROLELEVELNAME = this.ruleForm.ROLELEVELNAME
        resBody.data.PageFlag = 4
        resBody.data.CurrPage = this.currpage;
        resBody.data.PageRowNum = this.pageSize;
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.tableData = response.BODY.struct
          this.tableTotal = response.BODY.TotalRowNum
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      // 重置查询条件
      clearForm(item) {
        // 重置表格
        this.$refs["" + item].resetFields();
      },
    }
  }

</script>

<style rel="stylesheet/scss" lang="scss" scoped>


</style>
